package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.InstrumentAction;
import com.bean.MessageBean;
import com.exception.DBException;
import com.exception.InstrumentException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class InstrumentQueryServlet extends HttpServlet
{
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException
        {
                String key = request.getParameter("key");
                String value = request.getParameter("value");
                
                if(null == key || null==value)
                {
                        HandleInfo.handle(request, response,  
                                        StateInfo.INNER_CONFIRM_FAIL);
                        return;
                }
                if("".equals(key) || "".equals(value))
                {
                        HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
                        return;
                }
                
                if(!"name".equals(key) && !"barcode".equals(key))
                {
                        HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
                        return;
                }
                
                if ("barcode".equals(key) && !Regex.checkBarcode(value))
                {
                        HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
                        return;
                }
                
                MessageBean message = new MessageBean();
                message.setKey(key);
                message.setValue(value);
                
                try
                {
                        new InstrumentAction().queryInstrument(request, response, message);
                }
                catch (DBException e)
                {
                        HandleInfo.handle(request, response, e.getMessage());
                        return;
                }
                catch (InstrumentException e)
                {
                        HandleInfo.handle(request, response,e.getMessage());
                        return;
                }
        }

        
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException
        {
                doGet(request, response);
        }

}
